<?php
$params = array(
	'root'	=>	$this->params['named']['root']
);

if (isset($this->params['plugin'])) {
	$params['plugin'] = $this->params['plugin'];
} else {
	$params['plugin'] = null;
}


//debug(base64_encode(serialize($params)));exit;
?>
<?php if (isset($relatedInfo) && isset($relatedInfo['hasMany']) && !empty($relatedInfo['hasMany'])):?>
	<?php foreach ($relatedInfo['hasMany'] as $key => $item):
	if (strpos($item['model'], '.') !== false) {
		list($plugin, $modelClass) = explode('.', $item['model']);
	} else {
		$plugin = null;
		$modelClass = $item['model'];
	}
	?>
	
	<?php if (!isset($related_pages[$key])):?>
	<div class='cake_form'>
	<form>
	<fieldset>
		<legend><?php echo $item['label']?></legend>
		<div class='index' style='text-align:center'>
			<b><?php echo __("No items.");?></b>
		</div>
		<p align='right'>
			[ <a href="<?php echo $html->url(array(
				'plugin' 		=> $item['url']['plugin'], 
				'controller' 	=> $item['url']['controller'], 
				'action'		=> 'add',
 				'related_group'	=> $key,
 				'related_id'	=> $this->data[$models['Page']['name']]['id'],
 				'back_url'		=> base64_encode($this->here)
				))?>"><?php __("Click here to add new related page")?></a> ]
		</p>
	</fieldset>
	</form>
	</div>
	<?php else:
	$relatedConfigs = $related_pages[$key]['configs'];
	?>
		<div class='cake_form list_related_pages'>
			<?php echo $form->create($modelClass, array(
					'url'	=>	array(
						'plugin'	=>	isset($item['url']['plugin']) ? $item['url']['plugin'] : null,
						'controller'	=>	$item['url']['controller'],	
						'action'		=>	'update_priority'
					)
				));
			?>
			<fieldset>
				<legend><?php echo $item['label']?></legend>
				<div class='index'>
				
				<table class= 'list form_background' cellspacing='1' cellpadding='0' id='room_type_list'>
					<tr>
						<th width='23'><a><?php echo __("#"); ?></a></th>
						<th><a><?php echo __("Title", true); ?></a></th>
						<th><a><?php echo __("Category", true); ?></a></th>
						<?php if (isset($relatedConfigs[$modelClass]['order']) 
							&& $relatedConfigs[$modelClass]['order']['orderable']
							):
						?>
						<th width='70'><a><?php echo __("Priority", true); ?></a></th>
						<?php endif;?>
						<th width='150'><a><?php echo __("Actions"); ?></a></th>
					</tr>
					<?php
						$i=0;
						$class = 'altrow';
						foreach ($related_pages[$key]['data'] as $page):
							$class = $class == "" ? "class='altrow'" : "";
							$page_id = $page[$modelClass]['id'];
							$i++;
					?>
					<tr <?php echo $class?> page_id="<?php echo $page_id?>">
						<td><?php echo $page_id?></td>
						<td>
							<span class='title'><?php echo $page[$modelClass]['title']?></span><br/>
							<span class='alias'><?php echo $page[$modelClass]['alias']?></span>
						</td>
						<td>
							<span class='field_category'><?php echo $page['Category']['name']?></span>
						</td>
						<?php if (isset($relatedConfigs[$modelClass]['order']) && $relatedConfigs[$modelClass]['order']['orderable']):?>
						<td><input class='priority' size='4' type='text' name="priority[<?php echo $page[$modelClass]['id']?>]" value='<?php echo $page[$modelClass]['priority']?>'/></td>
						<?php endif;?>
						<td class='actions'>
							<div class="sub_menu_list">
								<div class="cssdropdown">
									<ul>
										<li class="headlink">
											<a href="#"><?php __("Options")?></a>
											<ul rel='<?php echo $page_id?>'>
				 							<li><a class="list_03 edit_page_link" page_id="<?php echo $page_id?>" href="<?php echo $html->url(array(
											'plugin' 		=> $item['url']['plugin'], 
											'controller' 	=> $item['url']['controller'], 
											'action'		=> 'edit',
				 							$page_id,
				 							'related_group'	=> $key,
				 							'related_id'	=> $this->data[$models['Page']['name']]['id'],
				 							'back_url'		=> base64_encode($this->here)
											))?>"><?php __("Edit");?></a></li>
											 <li>
											 <a class="list_04 delete_page" onclick="return confirm('<?php __("Are you sure want to delete this page?")?>')" 
											 href="<?php echo $html->url(array(
											'plugin' 		=> $item['url']['plugin'], 
											'controller' 	=> $item['url']['controller'], 
											'action'		=> 'delete',
											$page_id ,
				 							'related_group'	=> $key,
				 							'related_id'	=> $this->data[$models['Page']['name']]['id'],
				 							'back_url'		=> base64_encode($this->here)
											))?>"><?php __("Delete");?></a></li>
											</ul>
										</li>
									</ul>
								</div>
							</div>
						</td>
					</tr>
					<?php endforeach;?>
					
					<?php if ($paginator->params['paging'][$modelClass]['pageCount'] > 1):?>
					<tr class='list_footer'>
						<td colspan='3'>
							<?php echo $this->element('pagination/pagination', array('model' => $modelClass));?>
						</td>
						<?php if (isset($relatedConfigs[$modelClass]['order']) && $relatedConfigs[$modelClass]['order']['orderable']):?>
						<td>
							<input class='button' type='submit' value='<?php echo __("Update");?>'/>
						</td>
						<?php endif;?>
						<td></td>
					</tr>
					<?php endif;?>
				</table>
				
				</div>
				<p align='right'>
					[ <a href="<?php echo $html->url(array(
						'plugin' 		=> $item['url']['plugin'], 
						'controller' 	=> $item['url']['controller'], 
						'action'		=> 'add',
 						'related_group'	=> $key,
 						'related_id'	=> $this->data[$models['Page']['name']]['id'],
 						'back_url'		=> base64_encode($this->here)
						))?>"><?php __("Click here to add new related page")?></a> ]
				</p>
			</fieldset>
			<?php echo $form->end();?>
		</div>
		<?php endif;?>
	<?php endforeach;?>
<?php endif;?>